$(".yes_login").click(function () {
    var text_account=$("#account").val();
    var text_password=$("#password").val();
    var text_code=$("#code").val();
    
    //格式判断
    if(!vertify(text_account)){
        alert("请输入用户名");
        sCode();
        return;
    }
    if(!vertify(text_password)){
        alert("请输入密码");
        sCode();
        return;
    }
    //验证码格式判断
    if(!(/^[A-Za-z0-9]{4}$/.test(text_code))){
        alert("请输入验证码");
        sCode();
        return;
    }
    

    //提交
    $.ajax({
        url:'logon.php',
        type:'POST',
        data:{
            account:text_account,
            password:text_password,
            code:text_code
        },
        success :function(data) {
            if(data.status){
                // var link=window.location.href;
                // var reg=/login.html/i;
                // link.replace(reg,'index.html');
                // window.location.href=link;
                location.href='index.html';
                document.cookie="account="+text_account;
            }
            else {
                alert(data.msg);
                sCode();
            }
        },
        error:function (data) {
            alert('请求出错');
            console.log(data);
        }
    });
});

//验证函数
function vertify(text) {
    var pattern=/^[a-zA-Z0-9_]+$/;
    return pattern.test(text);

}
$(document).keyup(function(event){
    if(event.keyCode == 13){
        $(".yes_login").trigger("click");
    }
});
//登陆错误验证码刷新函数
function sCode(){
	$("#securityCode").attr("src","securityCodeImage.php?timestamp="+new Date().getTime());
}
//点击验证码刷新函数
$(function (){
	 $("#securityCode").click(function(){
	        $(this).attr("src","securityCodeImage.php?timestamp="+new Date().getTime());
	    });
});

//检查用户是否登录
function checkCookies() {
    if(document.cookie.match(/account=[^"].+;?/)){
        return true;
    }
    else {
        return false;
    }
}
//页面加载时执行
$("document").ready(function () {
    if (checkCookies()) {
        location.href = 'index.html';
    }
});